const app = getApp();
const util = require('../../utils/util.js')
Page({
    data: {
        backPage: 1
    },
    onLoad: function (options) {
        //请求分享数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
            data: {},
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == '200') {
                    this.setData({
                        share_default_title: res.data.datas.title,
                        share_default_image_url: res.data.datas.imageUrl
                    })
                }
            }
        })
        if (options.address_id) {
            this.setData({
                address_id: options.address_id
            })
        }
        if (options.backPage) {
            this.setData({
                backPage: options.backPage * 1
            })
        }
        util.share();
        var self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    key: res.data,
                })
            }
        })
        var curPages = getCurrentPages();
        var prePages = curPages[curPages.length - 2];
        if (prePages != undefined) {
            this.setData({
                address_id: prePages.data.address_id
            })
        }
    },

    onShow: function () {
        var self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                wx.request({
                    url: app.globalData.ApiUrl + '/index.php?act=member_address&op=address_list',
                    data: {
                        key: res.data,
                        chain_id: ''
                    },
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    dataType: 'json',
                    success: (res) => {
                        if (res.data.code == 200) {
                            self.setData({
                                address_list: res.data.datas.address_list
                            })
                        }
                    }
                });
            }
        })


    },
    
    editAddr: function (e) {
        let addressid = e.currentTarget.dataset.addressid;
        wx.redirectTo({
            url: '../address_add/address_add?addressid=' + addressid
        })
    },
    addressAdd: function () {
        wx.redirectTo({
            url: '../address_add/address_add'
        })
    },
    delAddr: function (e) {
        var self = this;
        wx.showModal({
            title: '删除地址',
            content: '确认删除吗？',
            success(res) {
                if (res.confirm) {
                    wx.request({
                        url: app.globalData.ApiUrl + '/index.php?act=member_address&op=address_del',
                        data: {
                            key: self.data.key,
                            address_id: e.currentTarget.dataset.addressid
                        },
                        method: 'POST',
                        header: {
                            'content-type': 'application/x-www-form-urlencoded'
                        },
                        dataType: 'json',
                        success: (res) => {
                            if (res.data.code == 200) {
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'success',
                                    duration: 1000,
                                    success: function () {
                                        setTimeout(function () {
                                            let address_list_new = self.data.address_list;
                                            address_list_new.splice(e.currentTarget.dataset.index, 1);
                                            self.setData({
                                                address_list: address_list_new
                                            })
                                        }, 1000);
                                    }
                                })
                            }
                        }
                    });
                }
            }
        })
    },
    onShareAppMessage: function () {
        let share_key = this.data.key ? this.data.key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.share_default_title,
            imageUrl: this.data.share_default_image_url,
            path: share_path
        }
    }
})